/*
 * @Author: lg
 * @Date: 2024-05-10 16:09:19
 * @LastEditors: lg
 * @LastEditTime: 2024-09-13 08:58:36
 * @Description:
 * @FilePath: \ssii-user-bk\src\main.ts
 */
import { createApp } from 'vue';
import App from './App.vue';
import './styles/index.scss';
import router from './router';
import './router/permission/index';
import 'virtual:svg-icons-register';
import setupComponents from '@/components/index';

import pinia from './store';

import {
  renderWithQiankun,
  qiankunWindow
} from 'vite-plugin-qiankun/dist/helper';

export let app: any;

// 挂载函数
function render(props: any) {
  const { container = '' } = props;
  app = createApp(App);

  //注册全局组件
  setupComponents(app);

  const useInstallArr: Array<any> = [router, pinia];

  useInstallArr.forEach((item) => {
    app.use(item);
  });

  app.mount(
    container ? container.querySelector('#ssii-document') : '#ssii-document'
  );
  if (qiankunWindow.__POWERED_BY_QIANKUN__) {
    //如果是qiankun启动
    // const { defaultSettings, baseStore } = props;
    // app.use(baseStore);
    console.log('传递过来的参数--------', props);
  }
}

//非qiankun启动
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  console.log('非qiankun----------启动');
  render({});
} else {
  console.log('qiankun----------启动');
  //qiankun启动
  renderWithQiankun({
    // 子应用挂载
    mount(props) {
      render(props);
    },
    // 只有子应用第一次加载会触发
    bootstrap() {
      // console.log('bootstrap');
    },
    // 卸载
    unmount() {
      console.log('unmount');
      app.unmount();
    },
    // 更新
    update() {
      console.log('update');
    }
  });
}
